/*
package com.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Script;
import co.elastic.clients.elasticsearch._types.StoredScriptId;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.indices.Alias;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

import java.io.IOException;

public class EsClientTest {

    public static void main(String[] args) {
        try {
            //createIndex();
            //addData();
            //delDataVidId("1003");
            //delDataVidQuery("苹果手机");
            //query("苹果手机");
            //updateViaId("1003");
            updateViaQuery("苹果手机", 9999);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // TODO 待完善
    private static void updateViaQuery(String value, Integer price) throws IOException {
        ElasticsearchClient esClient = initClient();
        UpdateByQueryResponse updateByQueryResponse = esClient.updateByQuery(updateByQuery -> updateByQuery.query(query ->
                query.match(MatchQuery.of(builder -> builder.field("productName.keyword").query(value))))
                .script(Script.of(s -> s.stored(StoredScriptId.of(fn ->
                        fn.params("price", JsonData.of(price)).id(""))))));
        System.out.println("根据条件修改：" + updateByQueryResponse.updated());
    }

    private static void updateViaId(String id) throws IOException {
        ElasticsearchClient esClient = initClient();
        ProductInfo productInfo = new ProductInfo("苹果手机", "苹果智能手机，价格实惠便宜", "黑色", 5999);
        UpdateResponse<ProductInfo> updateResponse = esClient.update(updateRequest -> updateRequest.index("product_info").id(id).doc(productInfo), ProductInfo.class);
        System.out.println("根据 ID 修改：" + updateResponse.result());
    }

    private static void query(String value) throws IOException {
        ElasticsearchClient esClient = initClient();
        SearchResponse<ProductInfo> searchResponse = esClient.search(searchRequest ->
                searchRequest.index("product_info").query(q -> q.match(MatchQuery.of(builder ->
                        builder.field("productName").query(value)))), ProductInfo.class);
        searchResponse.hits().hits().forEach(productInfoHit -> System.out.println(productInfoHit));
    }

    private static void delDataVidQuery(String value) throws IOException {
        ElasticsearchClient esClient = initClient();
        DeleteByQueryResponse deleteByQueryResponse = esClient.deleteByQuery(deleteByQuery ->
                deleteByQuery.index("product_info").query(query ->
                        query.match(MatchQuery.of(builder -> builder.field("productName.keyword").query(value)))));
        System.out.println("按照条件删除索引数据结果：" + deleteByQueryResponse.deleted());
    }

    private static void delDataVidId(String id) throws IOException {
        ElasticsearchClient esClient = initClient();
        DeleteResponse deleteResponse = esClient.delete(deleteReqest -> deleteReqest.index("product_info").id(id));
        System.out.println("按照 ID 删除索引数据结果：" + deleteResponse.result());
    }

    private static void addData() throws IOException {
        ElasticsearchClient esClient = initClient();
        ProductInfo productInfo = new ProductInfo("小米手机", "小米，智能手机，价格实惠便宜", "黑色", 2999);
        CreateResponse createResponse = esClient.create(createRequest -> createRequest.id("1002").index("product_info").document(productInfo));
        System.out.println("添加索引请求结果：" + createResponse.result());
    }

    */
/**
     * 创建索引
     *//*

    private static void createIndex() throws IOException {
        ElasticsearchClient esClient = initClient();
        CreateIndexResponse createIndexResponse = esClient.indices().create(
                new CreateIndexRequest.Builder()
                        .index("product_info")
                        .aliases("product_info_alias",
                                new Alias.Builder().isWriteIndex(true).build()
                        ).build());
        if(createIndexResponse.acknowledged()){
            System.out.println("创建索引成功！");
        } else {
            System.out.println("创建索引失败！");
        }
    }

    */
/**
     * 初始化客户端
     * @return
     *//*

    public static ElasticsearchClient initClient(){
        String hostname = "192.168.0.67";
        int port = 9200;
        String username = "elastic";
        String password = "1BZTIs85w3fOovvdFodM";
        // 基本的用户名密码认证
        BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
        basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostname, port, "http"));
        restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder ->
                httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider));
        RestClient restClient = restClientBuilder.build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        return esClient;
    }

}
*/
